SkillSpellFly = class("SkillSpellFly")

function SkillSpellFly:ctor( ... )
	local arg = {...}
	if(#arg ~= 4) then
		GameLog.Error("SkillSpellFly ctor param number is not 4.")
		return
	end

	self.id = arg[1]
	self.spellData = arg[2]
	self.role = arg[3]
	self.target = arg[4]
end

function SkillSpellFly:play()
	self.updateTime = 0

	self.res = ObjectPoolManager:GetObjectForce(self.spellData.res_name)
	self.transform = self.res:GetComponent(typeof(UnityEngine.Transform))
	if(not self.role.isFriend) then
		self.transform.eulerAngles = Vector3.New(0,180,0)
	end

	self:bind()
	self:calTarget()

	--[[
	self:updatePos(self.role.transform.position)
	self.targetTransform = self.target.transform
	]]
	--self.animationController = self.res:animationController(typeof(SpineAnimationController))
	--self.animationController.PlayAnimation(self.spellData.animName)
	self.isPlaying = true
	
	UpdateBeat:Add(self.update, self)
end

function SkillSpellFly:bind()
	local spellNodeTransform = self.role.transform:Find(self.spellData.start_node)
	local position = spellNodeTransform:TransformPoint(Vector3.New(self.spellData.start_offset.x, self.spellData.start_offset.y, 0))
	self:updatePos(position)
end

function SkillSpellFly:calTarget()
	local hitNodeTransform = self.target.transform:Find(self.spellData.end_node)
	local position = hitNodeTransform:TransformPoint(Vector3.New(self.spellData.end_offset.x, self.spellData.end_offset.y, 0))
	self.targetPos = position
end

function SkillSpellFly:updatePos(pos)
	local position = pos
	position.z = BattleSort.calZValue(position.x, position.y)
	position.z = BattleSort.calZValueWithShift(position.z, "spell_fly")
	self.transform.position = position
end

function SkillSpellFly:update()
	if(self.isPlaying) then
		local pos = self.transform.position
		--GameLog.Log(" pos is "..pos.x.." "..pos.y.." "..pos.z)
		--local targetPos = self.targetTransform.position
		local targetPos = self.targetPos
		pos.z = targetPos.z
		--GameLog.Log(" pos is "..targetPos.x.." "..targetPos.y.." "..targetPos.z)
		local vec = targetPos - pos
		local distance = vec:Magnitude()
		local moveDistance = self.spellData.speed * Time.deltaTime
		if(distance <= moveDistance) then
			--GameLog.Warning("spell fly end "..self.role.name )
			--self.transform.position = targetPos
			self:updatePos(targetPos)
			self.isPlaying = false
			if(self.spellData.damage ~= nil) then
				self.role:triggerDamage(self.spellData.damage, self.target)
			end
			ActionPlayer.removeSpellFly(self.id)
		else
			--GameLog.Log("spell fly")
			vec:SetNormalize()
			vec:Mul(moveDistance)

			--self.transform.position = pos + vec
			self:updatePos(pos + vec)
		end
	end
end

function SkillSpellFly:destroy()
	--GameLog.Warning("SkillSpellFly:destroy role is "..self.role.name.." self res name "..self.res.name)
	ObjectPoolManager:ReturnObject(self.spellData.res_name, self.res)

	self.spellData = nil
	self.role = nil
	self.target = nil

	self.res = nil
	self.transform = nil
	--self.targetTransform = nil

	UpdateBeat:Remove(self.update, self)
end